import { Message } from 'element-ui'
export const roomTypes = [
  { value: 0, text: 'home.1v1', path: 'one-to-one' },
  { value: 1, text: 'home.mini_class', path: 'small-class' },
  { value: 2, text: 'home.large_class', path: 'big-class' }
]

export const GlobalState = {
  loading: Boolean,
  lock: Boolean,
  toast: {
    type: String,
    message: String
  },
  dialog: {
    type: String,
    message: String,
    confirmText: String,
    cancelText: String
  },
  uploadNotice: {
    type: String,
    title: String
  },
  notice: {
    text: String,
    reason: String
  },
  nativeWindowInfo: {
    visible: Boolean,
    items: []
  },
  active: String,
  language: String,
  newMessageCount: Number,
  coVideo: Boolean
}

export class GlobalStore {
  state = GlobalState
  defaultState = {
    loading: false,
    toast: {
      type: '',
      message: ''
    },
    dialog: {
      type: '',
      message: '',
      confirmText: 'confirm',
      cancelText: 'cancel'
    },
    uploadNotice: {
      type: '',
      title: ''
    },
    notice: {
      reason: '',
      text: ''
    },
    nativeWindowInfo: {
      visible: false,
      items: []
    },
    lock: false,
    active: 'chatroom',
    language: navigator.language,
    newMessageCount: 0,
    coVideo: true
  }

  constructor() {
    this.state = this.defaultState
  }

  updateState(rootState) {
    this.state = {
      ...this.state,
      ...rootState
    }
  }

  showNotice({ reason, text }) {
    this.state = {
      ...this.state,
      notice: {
        text,
        reason
      }
    }
  }

  removeNotice() {
    this.state = {
      ...this.state,
      notice: {
        text: '',
        reason: ''
      }
    }
  }

  setNativeWindowInfo({ visible, items }) {
    this.state = {
      ...this.state,
      nativeWindowInfo: {
        visible,
        items
      }
    }
  }

  showUploadNotice({ type, title }) {
    this.state = {
      ...this.state,
      uploadNotice: {
        type,
        title
      }
    }
  }

  removeUploadNotice() {
    this.state = {
      ...this.state,
      uploadNotice: {
        type: '',
        title: ''
      }
    }
  }

  showToast({ type, message }) {
    this.state = {
      ...this.state,
      toast: {
        type,
        message
      }
    }
    Message(message)
  }

  showVideo(n) {
    this.state = {
      ...this.state,
      coVideo: n
    }
  }

  showDialog({ type, message }) {
    this.state = {
      ...this.state,
      dialog: {
        type,
        message
      }
    }
  }

  removeDialog() {
    this.state = {
      ...this.state,
      dialog: {
        type: '',
        message: ''
      }
    }
  }

  showLoading() {
    this.state = {
      ...this.state,
      loading: true
    }
  }

  stopLoading() {
    this.state = {
      ...this.state,
      loading: false
    }
  }

  setLanguage(language) {
    this.state = {
      ...this.state,
      language
    }
    window.location.reload()
  }

  setActive(active) {
    if (active !== 'chatroom') {
      this.state = {
        ...this.state,
        active
      }
    } else {
      this.state = {
        ...this.state,
        active,
        newMessageCount: 0
      }
    }
  }

  setMessageCount(len) {
    this.state = {
      ...this.state,
      newMessageCount: len
    }
  }

  lock() {
    this.state = {
      ...this.state,
      lock: true
    }
  }

  unlock() {
    this.state = {
      ...this.state,
      lock: false
    }
  }
}
export const globalStore = new GlobalStore()
